package com.rmy.queue.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.rmy.common.core.text.Convert;
import com.rmy.common.utils.StringUtils;
import com.rmy.queue.domain.QmsMainBusiness;
import com.rmy.queue.domain.QmsWindowInfo;
import com.rmy.queue.service.IQmsWindowInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.rmy.common.annotation.Log;
import com.rmy.common.core.controller.BaseController;
import com.rmy.common.core.domain.AjaxResult;
import com.rmy.common.enums.BusinessType;
import com.rmy.queue.domain.QmsQueueList;
import com.rmy.queue.service.IQmsQueueListService;
import com.rmy.common.utils.poi.ExcelUtil;
import com.rmy.common.core.page.TableDataInfo;

/**
 * 排队队列Controller
 * 
 * @author rmy
 * @date 2022-09-29
 */
@RestController
@RequestMapping("/queue/queuelist")
public class QmsQueueListController extends BaseController
{
    @Autowired
    private IQmsQueueListService qmsQueueListService;

    @Autowired
    private IQmsWindowInfoService qmsWindowInfoService;

    /**
     * 查询排队队列列表
     */
    @PreAuthorize("@ss.hasPermi('queue:queuelist:list')")
    @GetMapping("/list")
    public TableDataInfo list(QmsQueueList qmsQueueList)
    {
        startPage();
        List<QmsQueueList> list = qmsQueueListService.selectQmsQueueListList(qmsQueueList);
        return getDataTable(list);
    }

    /**
     * 导出排队队列列表
     */
    @PreAuthorize("@ss.hasPermi('queue:queuelist:export')")
    @Log(title = "排队队列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QmsQueueList qmsQueueList)
    {
        List<QmsQueueList> list = qmsQueueListService.selectQmsQueueListList(qmsQueueList);
        ExcelUtil<QmsQueueList> util = new ExcelUtil<QmsQueueList>(QmsQueueList.class);
        util.exportExcel(response, list, "排队队列数据");
    }

    /**
     * 获取排队队列详细信息
     */
    @PreAuthorize("@ss.hasPermi('queue:queuelist:query')")
    @GetMapping(value = "/{serialNo}")
    public AjaxResult getInfo(@PathVariable("serialNo") String serialNo)
    {
        return AjaxResult.success(qmsQueueListService.selectQmsQueueListBySerialNo(serialNo));
    }

    /**
     * 新增排队队列
     */
    @PreAuthorize("@ss.hasPermi('queue:queuelist:add')")
    @Log(title = "排队队列", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QmsQueueList qmsQueueList)
    {
        return toAjax(qmsQueueListService.insertQmsQueueList(qmsQueueList));
    }

    /**
     * 修改排队队列
     */
    @PreAuthorize("@ss.hasPermi('queue:queuelist:edit')")
    @Log(title = "排队队列", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QmsQueueList qmsQueueList)
    {
        return toAjax(qmsQueueListService.updateQmsQueueList(qmsQueueList));
    }

    /**
     * 删除排队队列
     */
    @PreAuthorize("@ss.hasPermi('queue:queuelist:remove')")
    @Log(title = "排队队列", businessType = BusinessType.DELETE)
	@DeleteMapping("/{serialNos}")
    public AjaxResult remove(@PathVariable String[] serialNos)
    {
        return toAjax(qmsQueueListService.deleteQmsQueueListBySerialNos(serialNos));
    }


        /**
         * 柜员叫号
         */
        @Log(title = "柜员叫号", businessType = BusinessType.UPDATE)
        @PostMapping("/callnumber")
        public AjaxResult callnumber(@RequestParam String windowUuid) throws Exception {
            QmsWindowInfo qmsWindowInfo = qmsWindowInfoService.selectQmsWindowInfoByUUID(windowUuid);
            if (qmsWindowInfo == null) {
                return AjaxResult.error("窗口信息不存在，请检查记号工作人员登录路径！！");
            }

            QmsQueueList qmsQueueList = new QmsQueueList();
            qmsQueueList.setCallerId(getUsername());
            qmsQueueList.setOrgNo(Convert.toStr(getDeptId()));
            qmsQueueList.setWindowId(qmsWindowInfo.getId());

            String strSerialNo = qmsQueueListService.callNumber(qmsQueueList);
            if ("-1".equals(strSerialNo)) {
                return AjaxResult.error("当前无排队信息，无需叫号！");
            }
            return AjaxResult.success((Object)strSerialNo);
        }

}
